package com.wyth.lottery.utils;


import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

import android.graphics.Bitmap;

/**
 * Created by Administrator on 2017/11/13.
 */

public class QRUtils {
    /**
     * 生成二维码
     *
     * @param input       二维码内容
     * @param width       宽度
     * @param height      高度
     * @param ic_launcher 需要加的图标
     * @param islogo      是否加图标
     * @return bitmap  返回二维码bitmap
     */
    public static Bitmap enCode(String input, int width, int height, int ic_launcher, boolean islogo) {
        int WIDTH, HEIGHT;
        WIDTH = width;
        HEIGHT = height;
        MultiFormatWriter formatWriter = new MultiFormatWriter();
        Bitmap bitmap = null;
//        Hashtable hints = new Hashtable();
//        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        try {
            // 按照指定的宽度，高度和附加参数对字符串进行编码
            BitMatrix bitMatrix = formatWriter.encode(new String(input.getBytes("UTF-8"), "ISO-8859-1"), BarcodeFormat.QR_CODE, WIDTH, HEIGHT/*, hints*/);
            bitmap = bitMatrix2Bitmap(bitMatrix);
            if (islogo) {
                bitmap = addLogo(bitmap, BitmapFactory.decodeResource(MyApplication.getContext().getResources(), ic_launcher));
            }
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;

    }

    private static Bitmap addLogo(Bitmap qrBitmap, Bitmap logoBitmap) {
        int qrBitmapWidth = qrBitmap.getWidth();
        int qrBitmapHeight = qrBitmap.getHeight();
        int logoBitmapWidth = logoBitmap.getWidth();
        int logoBitmapHeight = logoBitmap.getHeight();
        Bitmap blankBitmap = Bitmap.createBitmap(qrBitmapWidth, qrBitmapHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(blankBitmap);
        canvas.drawBitmap(qrBitmap, 0, 0, null);
        canvas.save(Canvas.ALL_SAVE_FLAG);
        float scaleSize = 1.0f;
        while ((logoBitmapWidth / scaleSize) > (qrBitmapWidth / 5) || (logoBitmapHeight / scaleSize) > (qrBitmapHeight / 5)) {
            scaleSize *= 2;
        }
        float sx = 1.0f / scaleSize;
        canvas.scale(sx, sx, qrBitmapWidth / 2, qrBitmapHeight / 2);
        canvas.drawBitmap(logoBitmap, (qrBitmapWidth - logoBitmapWidth) / 2, (qrBitmapHeight - logoBitmapHeight) / 2, null);
        canvas.restore();
        return blankBitmap;
    }

    private static Bitmap bitMatrix2Bitmap(BitMatrix matrix) {
        int w = matrix.getWidth();
        int h = matrix.getHeight();
        int[] rawData = new int[w * h];
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                int color = Color.WHITE;
                if (matrix.get(i, j)) {
                    color = Color.BLACK;
                }
                rawData[i + (j * w)] = color;
            }
        }

        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
        bitmap.setPixels(rawData, 0, w, 0, 0, w, h);
        return bitmap;
    }


}
